package id.co.rho.scoring.cid.client.widgets;

import id.co.rho.scoring.cid.client.model.ChangeTrackingModel;

import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;

public class ChangeTrackingSupportSimpleComboBinding extends SimpleComboBoxFieldBinding {

	public ChangeTrackingSupportSimpleComboBinding(SimpleComboBox field,
			String property) {
		super(field, property);
	}

	/**
	 * Will update the field and update originalValue based on ChangeTrackingModel's originalValue,
	 * ignoring the parameter updateOriginalValue.
	 * If the model is not of type ChangeTrackingModel than revert to original mechanism.
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void updateField(boolean updateOriginalValue) {
		if(getModel() instanceof ChangeTrackingModel) {
			ChangeTrackingModel model = (ChangeTrackingModel) getModel();
			Object val = onConvertModelValue(model.get(property));
			Object originalVal = onConvertModelValue(model.getOriginal(property));

		    field.setValue(val);
		    field.setOriginalValue(originalVal);
		    
		    if(field instanceof DirtyAwareSimpleComboBox){
		    	((DirtyAwareSimpleComboBox)field).resetDirtyness();
		    }
		}else {
			super.updateField(updateOriginalValue); 
		}
	}
}
